Skip to main content

Token Farm

The TokenFarm comprises the majority of V0 functionality. Users interact with the deposit and withdraw functions found in TokenFarmFacet, when they do, the following things happen. Firstly, the farm in question is updated with the update function in ToolShedFacet. This mints the Sdex for the block and calculates how much of the Sdex is alotted to this pool in question. It than updates the {PoolInfo} {TokenData} accSdexPerShare which counts the accumulated Sdex of that pool per every unit of token provided for it. a {UserPosition} is than created according to the depositors parameters that is stored in the {UserInfo} {UserPositions} array of that user. For each token in the pool, the blocksStaked times the stakedAmount is added to the {RewardGlobals} to keep track of the proportion of rewards at the end.

When the 'withdraw' function is called. it firsts checks to see if the {UserPosition} has an nft applied, if so, the function is highjacked by the {RewardFacets} version of the function to implement the custom logic that lands up doleing out the rewards. Otherwise the system than checks if the block.number is before or after the endBlock of the {UserPosition} to see if a premature withdrawal is occuring.

If the function is a premature withdraw, the calcRefund function in ToolShedFacet. is executed to determine how much is refunded to the user for that token, and how much is sent to the penalty pool for future rewards. For V0 this function is rather intense, starting with a 100% penalty, linearlly decreasing to 0% at timeEnd, in future versions we have a different algorithm in mind all together. In this event, any SDEX that has been accrued by the position is also transferred to the accruedSdexPenalty pool.

caution

While a proportion of your staked tokens are return based on the % time ones completed of their commitment, 100% of all accrued Sdex are penalty pooled in the case of a premature withdraw

In the case a user is withdrawing after thier endBlock not only is 100% of each of the staked tokens and 100% of the accrued SDEX is returned. But the each staked tokens' blocksStaked and amount is multiplied together and sent to the RewardFacet's requestReward function. This function calculates what proportion of the penalty pool of that token the user receives in their NFT reward. After the amount is calculated. It is sent to the mintReward function which is tasked with Pseudorandomly choosing which NFT to mint for that token out of the list of {ValidRewardsForToken} list.

note

v0 uses a pseudorandom function generator to determine exactly which NFT is minted for the user. Savvy individuals will be able to 'hack' this value to obtain their desired one. Future Sigmadex versions will provide much better randomness through the Chainlink VRF

As no function overlapped are permitted in the Diamond Architecture, the reward function than delegates minting by function signature to the NFT reward to be created for the User.

Additionally, an Sdex NFT reward is also minted for the user. Since it was accrued SDEX the TokenFarm calls the requestSdexReward

tip

The native token SDEX, has two separate penalty pools, one that acts like a normal tokens' penalty pool, and a special one for accrued SDEX. If you stake SDEX like a normal token, you pick from the first pool, if the reward comes from block rewards, it takes from this special pool